﻿/*
 * 版 本 v1.0
 * Copyright (c) 2013-2018 广州市同慧信息科技有限公司
 * 创建人：ftquan
 * 日 期：2017.08.04
 * 描 述：流程（我的任务）	
 */
var refreshGirdData;
var schemeCode = request("schemeCode");
var tabindex = request("categoryId");

var bootstrap = function ($, learun) {
    "use strict";
    var categoryId = '1';
    var logbegin = '';
    var logend = '';

    refreshGirdData = function () {
        page.search();
    }

    var page = {
        init: function () {
            $('#lr_verify').hide();
            page.initleft();
            page.initGrid();
            page.bind();
        },
        bind: function () {
            $('#datesearch').lrdate({
                dfdata: [
                    {
                        name: '今天', begin: function () {
                            return learun.getDate('yyyy-MM-dd 00:00:00')
                        }, end: function () {
                            return learun.getDate('yyyy-MM-dd 23:59:59')
                        }
                    },
                    {
                        name: '近7天', begin: function () {
                            return learun.getDate('yyyy-MM-dd 00:00:00', 'd', -6)
                        }, end: function () {
                            return learun.getDate('yyyy-MM-dd 23:59:59')
                        }
                    },
                    {
                        name: '近1个月', begin: function () {
                            return learun.getDate('yyyy-MM-dd 00:00:00', 'm', -1)
                        }, end: function () {
                            return learun.getDate('yyyy-MM-dd 23:59:59')
                        }
                    },
                    {
                        name: '近3个月', begin: function () {
                            return learun.getDate('yyyy-MM-dd 00:00:00', 'm', -3)
                        }, end: function () {
                            return learun.getDate('yyyy-MM-dd 23:59:59')
                        }
                    },
                ],
                // 月
                mShow: false,
                premShow: false,
                // 季度
                jShow: false,
                prejShow: false,
                // 年
                ysShow: false,
                yxShow: false,
                preyShow: false,
                yShow: false,
                // 默认
                dfvalue: '2',
                selectfn: function (begin, end) {
                    logbegin = begin;
                    logend = end;

                    page.search();
                }
            });

            // 搜索框初始化
            $('#txt_Keyword').on("keypress", function (e) {
                if (event.keyCode == "13") {
                    var $this = $(this);
                    var keyword = $this.val();
                    page.search({keyword: keyword});
                }
            });
            // 查询
            $('#btn_Search').on('click', function () {
                var keyword = $('#txt_Keyword').val();
                page.search({keyword: keyword});
            });
            // 刷新
            $('#lr_refresh').on('click', function () {
                location.reload();
            });
            // 查看流程进度
            $('#lr_eye').on('click', function () {
                    var processId = $('#gridtable').jfGridValue('id');
                    var taskId = $('#gridtable').jfGridValue('taskId');
                    var processName = $('#gridtable').jfGridValue('code') + $('#gridtable').jfGridValue('processName');
                    var taskName = $('#gridtable').jfGridValue('taskName');

                    if (learun.checkrow(processId)) {
                        if (config.isTabs()) { //主题1
                            learun.frameTab.open({
                                id: processId + taskId,
                                css: 'fa magic',
                                name: '查看流程进度【' + processName + '】',
                                path: config.webportal_server + 'pages/workflow/wfmyTask/custmerWorkFlowForm.html?tabIframeId=' + processId + taskId + '&type=100' + "&processId=" + processId + "&taskId=" + taskId + '&access_token=' + config.getToken().access_token
                            });
                        } else {
                            learun.layerForm({
                                id: processId + taskId,
                                title: '查看流程进度【' + processName + '】',
                                url: config.webportal_server + 'pages/workflow/wfmyTask/custmerWorkFlowForm.html?tabIframeId=' + processId + taskId + '&type=100' + "&processId=" + processId + "&taskId=" + taskId + '&access_token=' + config.getToken().access_token,
                                height: 1080,
                                width: 1920
                            });
                        }
                    }
                }
            );
            // 发起流程
            $('#lr_release').on('click', function () {
                learun.layerForm({
                    id: 'form',
                    title: '选择流程模板',
                    url: config.webportal_server + 'pages/workflow/wfmyTask/releaseForm.html?access_token=' + config.getToken().access_token,
                    height: 600,
                    width: 825,
                    maxmin: true,
                    callBack: function (id) {
                        return top[id].acceptClick();
                    }
                });
            });
            // 审核流程
            $('#lr_verify').on('click', function () {
                var processId = $('#gridtable').jfGridValue('id');
                var taskId = $('#gridtable').jfGridValue('taskId');
                var processName = $('#gridtable').jfGridValue('processName');
                var taskName = $('#gridtable').jfGridValue('taskName');
                var taskType = $('#gridtable').jfGridValue('taskType');
                if (taskType == 4) {
                    if (learun.checkrow(taskId)) {
                        if (config.isTabs()) { //主题1
                            learun.frameTab.open({
                                id: taskId,
                                css: 'fa magic',
                                name: '审核流程【' + processName + '/' + taskName + '】',
                                path: config.webportal_server + 'pages/workflow/wfmyTask/custmerWorkFlowForm.html?tabIframeId=' + taskId + '&type=4' + "&processId=" + processId + "&taskId=" + taskId + '&access_token=' + config.getToken().access_token
                            });
                        } else {
                            learun.layerForm({
                                id: taskId,
                                title: '审核流程【' + processName + '/' + taskName + '】',
                                url: config.webportal_server + 'pages/workflow/wfmyTask/custmerWorkFlowForm.html?tabIframeId=' + taskId + '&type=4' + "&processId=" + processId + "&taskId=" + taskId + '&access_token=' + config.getToken().access_token,
                                height: 1080,
                                width: 1920
                            });
                        }
                    }
                } else if (taskType == 1) {
                    if (learun.checkrow(taskId)) {
                        if (config.isTabs()) { //主题1
                            learun.frameTab.open({
                                id: taskId,
                                css: 'fa magic',
                                name: '审核流程【' + processName + '/' + taskName + '】',
                                path: config.webportal_server + 'pages/workflow/wfmyTask/custmerWorkFlowForm.html?tabIframeId=' + taskId + '&type=1' + "&processId=" + processId + "&taskId=" + taskId + '&access_token=' + config.getToken().access_token
                            });
                        } else {
                            learun.layerForm({
                                id: taskId,
                                title: '审核流程【' + processName + '/' + taskName + '】',
                                url: config.webportal_server + 'pages/workflow/wfmyTask/custmerWorkFlowForm.html?tabIframeId=' + taskId + '&type=1' + "&processId=" + processId + "&taskId=" + taskId + '&access_token=' + config.getToken().access_token,
                                height: 1080,
                                width: 1920
                            });
                        }
                    }
                } else if (taskType == 2) {
                    learun.alert.warning('请点击重新发起');
                } else {
                    if (learun.checkrow(taskId)) {
                        if (config.isTabs()) { //主题1
                            learun.frameTab.open({
                                id: taskId,
                                css: 'fa magic',
                                name: '审核流程【' + processName + '/' + taskName + '】',
                                path: config.webportal_server + 'pages/workflow/wfmyTask/custmerWorkFlowForm.html?tabIframeId=' + taskId + '&type=3' + "&processId=" + processId + "&taskId=" + taskId + '&access_token=' + config.getToken().access_token
                            });
                        } else {
                            learun.layerForm({
                                id: taskId,
                                title: '审核流程【' + processName + '/' + taskName + '】',
                                url: config.webportal_server + 'pages/workflow/wfmyTask/custmerWorkFlowForm.html?tabIframeId=' + taskId + '&type=3' + "&processId=" + processId + "&taskId=" + taskId + '&access_token=' + config.getToken().access_token,
                                height: 1080,
                                width: 1920
                            });
                        }
                    }
                }
            });
        },
        initleft: function () {
            $('#lr_left_list li').on('click', function () {
                var $this = $(this);
                var $parent = $this.parent();
                $parent.find('.active').removeClass('active');
                $this.addClass('active');

                categoryId = $this.attr('data-value');

                if (categoryId == '2') {
                    $('#lr_verify').show();
                } else {
                    $('#lr_verify').hide();
                }

                page.search();
            });

            if (!!tabindex) {
                categoryId = tabindex;
                $("#lr_left_list li[data-value='" + tabindex + "']").addClass('active');
                $("#lr_left_list li[data-value!='" + tabindex + "']").removeClass('active');
                if (categoryId == '2') {
                    $('#lr_verify').show();
                } else {
                    $('#lr_verify').hide();
                }

                page.search();
            }
        },
        initGrid: function () {
            $('#gridtable').jfGrid({
                url: config.base_server() + 'wftasks?access_token=' + config.getToken().access_token,
                headData: [
                    {
                        label: "任务", name: "taskName", width: 160, align: "left",
                        formatter: function (cellvalue, row, dfop, $cell) {
                            var isaAain = false;
                            if (categoryId == '1') {
                                if (row.isAgain == 1) {
                                    isaAain = true;
                                } else if (row.isFinished == 0) {
                                    if (row.status == -1) {
                                        isaAain = true;
                                    } else {
                                        // 加入撤销按钮
                                        $cell.on('click', '.revoke', function () {// 删除
                                            learun.layerConfirm('是否确认撤销流程？', function (res, _index) {
                                                if (res) {
                                                    learun.loading(true, '撤销流程...');
                                                    var postData = {
                                                        processId: row.id,
                                                    };
                                                    learun.httpAsync('Post', config.base_server() + 'wftasks/revoke/' + row.id + '?access_token=' + config.getToken().access_token, postData, function (data) {
                                                        learun.loading(false);
                                                        refreshGirdData();
                                                    });
                                                    top.layer.close(_index);
                                                }
                                            });
                                            return false;
                                        });
                                        var _btnHtml = '本人发起';
                                        if (row.status == 1 && row.isStart == 0) {
                                            _btnHtml += '<span class="label label-warning revoke" style="margin-left:5px;" title="撤销流程" >撤销流程</span>';
                                        }
                                        return _btnHtml;
                                    }
                                } else {
                                    return '本人发起';
                                }
                            }

                            if (row.taskType == 4) {
                                return "【加签】" + cellvalue;
                            } else if (row.taskType == 2) {
                                isaAain = true;
                            }
                            if (isaAain) {
                                $cell.on('click', '.reCreate', function () {
                                    if (config.isTabs()) { //主题1
                                        learun.frameTab.open({
                                            id: row.id,
                                            css: 'fa magic',
                                            name: '重新发起流程【' + row.processName + '】',
                                            path: config.webportal_server + 'pages/workflow/wfmyTask/custmerWorkFlowForm.html?processId=' + row.id + '&tabIframeId=' + row.id + '&type=2&access_token=' + config.getToken().access_token
                                        });
                                    } else {
                                        learun.layerForm({
                                            id: row.id,
                                            title: '重新发起流程【' + row.processName + '】',
                                            url: config.webportal_server + 'pages/workflow/wfmyTask/custmerWorkFlowForm.html?processId=' + row.id + '&tabIframeId=' + row.id + '&type=2&access_token=' + config.getToken().access_token,
                                            height: 1080,
                                            width: 1920
                                        });
                                    }
                                });
                                $cell.on('click', '.delete', function () {
                                    learun.layerConfirm('是否确认删除该流程？', function (res, _index) {
                                        if (res) {
                                            learun.loading(true, '删除中...');
                                            learun.httpAsync('Delete', config.base_server() + 'wftasks/delete/' + row.id + '?access_token=' + config.getToken().access_token, {}, function (data) {
                                                learun.loading(false);
                                                refreshGirdData();
                                            });

                                            top.layer.close(_index);
                                        }
                                    });
                                });
                                return "<span class=\"label label-warning reCreate\">重新发起</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class=\"label label-danger delete\">删除</span>";
                            }
                            if (categoryId == '3' && row.taskType == 1 && row.isFinished == 0 && row.canRevoke == 1) {// 已完成任务，添加一个撤销按钮
                                cellvalue = '<span class="label label-warning revoke2" style="margin-left:5px;margin-right:5px;" title="撤销审核" >撤销审核</span>' + cellvalue;

                                $cell.on('click', '.revoke2', function () {// 删除
                                    learun.layerConfirm('是否确认撤销审核？', function (res, _index) {
                                        if (res) {

                                            learun.loading(true, '撤销审核...');
                                            var postData = {
                                                processId: row.id,
                                                taskId: row.taskId,
                                            };
                                            learun.httpAsync('Post', config.base_server() + 'wfengines/revokeAudit?access_token=' + config.getToken().access_token, postData, function (data) {
                                                learun.loading(false);
                                                refreshGirdData();
                                            });

                                            top.layer.close(_index);
                                        }
                                    });
                                    return false;
                                });
                            }

                            return cellvalue;
                        }
                    },
                    {label: "流程单号", name: "code", width: 120, align: "center"},
                    {label: "标题", name: "processName", width: 220, align: "left"},
                    {label: "模板名称", name: "schemeName", width: 150, align: "left"},

                    {
                        label: "等级", name: "processLevel", width: 80, align: "center",
                        formatter: function (cellvalue) {
                            switch (cellvalue) {
                                case 0:
                                    return '普通';
                                    break;
                                case 1:
                                    return '重要';
                                    break;
                                case 2:
                                    return '紧急';
                                    break;
                            }
                        }
                    },
                    {
                        label: "状态", name: "status", width: 70, align: "center",
                        formatter: function (cellvalue, row) {
                            if (row.isFinished == 0) {
                                if (cellvalue == 1) {
                                    return "<span class=\"label label-success\">运行中</span>";
                                } else if (cellvalue == -1) {
                                    return "<span class=\"label label-default\">草稿</span>";
                                }
                            } else if (row.hasReject == 2) {
                                return "<span class=\"label label-danger\">拒绝</span>";
                            } else {
                                return "<span class=\"label label-warning\">结束</span>";
                            }
                        }
                    },
                    {label: "单位", name: "companyName", width: 200, align: "center"},
                    {label: "部门", name: "departmentName", width: 100, align: "center"},
                    {label: "发起者", name: "createUsername", width: 70, align: "center"},
                    {
                        label: "发起时间", name: "createTime", width: 150, align: "left",
                        formatter: function (cellvalue) {
                            return learun.formatDate(cellvalue, 'yyyy-MM-dd hh:mm:ss');
                        }
                    },
                    {label: "备注", name: "description", width: 300, align: "left"}

                ],
                dblclick: function (row) {
                    var processId = row.id;
                    var taskId = row.taskId;
                    var processName = row.code + " " + row.processName;
                    if (learun.checkrow(processId)) {
                        if (config.isTabs()) { //主题1
                            learun.frameTab.open({
                                id: processId + taskId,
                                css: 'fa magic',
                                name: '查看流程进度【' + processName + '】',
                                path: config.webportal_server + 'pages/workflow/wfmyTask/custmerWorkFlowForm.html?tabIframeId=' + processId + taskId + '&type=100' + "&processId=" + processId + "&taskId=" + taskId + '&access_token=' + config.getToken().access_token
                            });
                        } else {
                            learun.layerForm({
                                id: processId,
                                title: '查看流程进度【' + processName + '】',
                                url: config.webportal_server + 'pages/workflow/wfmyTask/custmerWorkFlowForm.html?tabIframeId=' + processId + taskId + '&type=100' + "&processId=" + processId + "&taskId=" + taskId + '&access_token=' + config.getToken().access_token,
                                height: 1080,
                                width: 1920
                            });
                        }
                    }
                },
                mainId: 'id',
                isPage: true,
                sidx: 'createTime'
            });
        },
        search: function (param) {
            param = param || {};
            param.startTime = logbegin;
            param.endTime = logend;
            param.categoryId = categoryId;
            if (!!schemeCode) {
                param.schemeCode = schemeCode;
            }

            $('#gridtable').jfGridSet('reload', param);
        }
    };

    page.init();
}


